<?php

$server = new Swoole\WebSocket\Server("0.0.0.0", 8812);
$server->set([
    'enable_static_handler' => true,
    'document_root' => '/data/swoole_mooc/data'
]);
//监听websocket
$server->on('open','onOpen');
function onOpen($server,$request) {
    print_r($request->fd);
}
// $server->on('open', function (Swoole\WebSocket\Server $server, $request) {
//     echo "server: handshake success with fd{$request->fd}\n";
// });

//监听websocket消息事件
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server" . $frame->data);
});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();